

let login = function login(obj) {
    // console.log(obj);
    const { loginService } = require('../../services/common/user');

    const { path, req, res, conn } = obj;

    let result = '';

    // 获取参数
    req.on('data', (chunk) => {
        result += chunk;
    });
    req.on('end', () => {
        console.log(path);

        let instance = JSON.parse(result);

        loginService({
            uPhone: instance.phone,
            uPwd: instance.password,
        }, conn, (bol, data) => {
            let rData = {};
            if (bol) {
                rData.status = 0;
                rData.statusText = 'OK';
                rData.info = data;
            } else {
                rData.status = 1;
                rData.statusText = 'NOT OK';
            }

            res.write(JSON.stringify(rData));
            res.end();
        });
        console.log(result);
    });

};

let logout = function signOut(obj) {
    console.log(obj);
    const { logoutService } = require('../../services/common/user');

    const { path, req, res, conn } = obj;

    let result = '';

    // 获取参数
    req.on('data', (chunk) => {
        result += chunk;
    });
    req.on('end', () => {
        console.log(path);

        console.log(result);
        let instance = JSON.parse(result);
        logoutService({
            uid: instance.id,
        }, conn, (bol) => {
            let rData = {};
            if (bol) {
                rData.status = 0;
                rData.statusText = 'OK';
            } else {
                rData.status = 1;
                rData.statusText = 'NOT OK';
            }
            res.write(JSON.stringify(rData));
            res.end();
        });
    });
};

let register = function register(obj) {
    // console.log(obj);

    const { path, req, res, conn } = obj;
    const { registerService } = require('../../services/common/user');
    let result = '';

    registerService(req, conn, (bol) => {
        let rData = {};
        if (bol) {
            rData.status = 0;
            rData.statusText = 'OK';
        } else {
            rData.status = 1;
            rData.statusText = 'NOT OK';
        }
        res.write(JSON.stringify(rData));
        res.end();
    });

};

let isLxUser = function isLxUser(obj) {
    // console.log(obj);

    const { path, req, res, conn } = obj;
    const { isLxUserService } = require("../../services/common/user");
    let result = '';

    // 获取参数
    req.on('data', (chunk) => {
        result += chunk;
    });
    req.on('end', () => {
        // console.log(path);

        // console.log(result);
        let instance = JSON.parse(result);
        isLxUserService({
            uPhone: instance.phone,
        }, conn, (bol) => {
            let rData = {};
            if (bol) {
                rData.status = 1;
                rData.statusText = 'NOT OK';
            } else {
                rData.status = 0;
                rData.statusText = 'OK';
            }
            res.write(JSON.stringify(rData));
            res.end();
        });
    });

};

module.exports = {
    login,
    logout,
    register,
    isLxUser,
};